var debug = false;
var serverPath = "http://localhost:8080/";
// var serverPath = "http://47.101.180.140:9080/";

if (debug) {
    serverPath = "http://localhost:8090/";
}

var  ht = window.innerHeight;
var  hw = window.innerWidth;

$("#body").height(ht);
$(".demo .layui-card").height(Number(ht-107)/4);
var tableHeight=Number(ht-166)/2;

//获取传入参数
function loadPageVar (sVar) {
    return decodeURI(window.location.search.replace(new RegExp("^(?:.*[&\\?]" + encodeURI(sVar).replace(/[\.\+\*]/g, "\\$&") + "(?:\\=([^&]*))?)?.*$", "i"), "$1"));
}

//获取数据
function getParam(){
    layui.use(['layer'], function(){
         layer.load(1);
        $.ajax({
            url: serverPath + "repairOrder/repairOrderDashBoard/repairList.action",
            data: {'enterpriseId':loadPageVar('enterpriseId')},
            async: false,
            timeout: 5000,
            type: "post",
            dataType: "json",
            success: function(result) {
                $(".enterprise").html(result.data.enterprise);
                $("#todayNumber").html(result.data.todayNumber);
                $("#todayPercent").html(result.data.todayCompletePercent);
                $("#todayAB").html(result.data.todayAB);
                $("#todayABPercent").html(result.data.todayABCompletePercent);
                $("#weekNumber").html(result.data.weekNumber);
                $("#weekPercent").html(result.data.weekCompletePercent);

                $("#monthNumber").html(result.data.monthNumber);
                $("#monthPercent").html(result.data.monthCompletePercent);

                $("#weekAB").html(result.data.weekAB);
                $("#weekABPercent").html(result.data.weekABCompletePercent);

                var orderList=result.data.todayOrders;
                var orderABList=result.data.todayABOrders;

                var max=Number(hw)*0.66/5+"px";
                var min=Number(hw)*0.66/12+"px";
                var mindium=Number(hw)*0.66/9+"px";

                for(var i=0;i<orderList.length;i++){

                    $("#tableId").append('<tr>\n' +
                        '                                        <td style="width: '+max+'">'+orderList[i].reportContent+'</td>\n' +
                        '                                        <td style="width: '+min+'">'+orderList[i].reportLevel+'</td>\n' +
                        '                                        <td style="width: '+min+'">'+orderList[i].majorName+'</td>\n' +
                        '                                        <td style="width: '+mindium+'">'+orderList[i].reportArea+'</td>\n' +
                        '                                        <td style="width: '+mindium+'">'+orderList[i].repairOrderType+'</td>\n' +
                        '                                        <td style="width: '+mindium+'">'+orderList[i].orderStatus+'</td>\n' +
                        '                                        <td style="width: '+mindium+'">'+orderList[i].createDate+'</td>\n' +
                        '                                        <td style="width: '+mindium+'">'+orderList[i].receiveUserName+'</td>\n' +
                        '                                    </tr>')
                }
                for(var i=0;i<orderABList.length;i++){

                    $("#tableAB").append('<tr>\n' +
                        '                                        <td style="width: '+max+'">'+orderABList[i].reportContent+'</td>\n' +
                        '                                        <td style="width: '+min+'">'+orderABList[i].reportLevel+'</td>\n' +
                        '                                        <td style="width: '+min+'">'+orderABList[i].majorName+'</td>\n' +
                        '                                        <td style="width: '+mindium+'">'+orderABList[i].reportArea+'</td>\n' +
                        '                                        <td  style="width: '+mindium+'">'+orderABList[i].repairOrderType+'</td>\n' +
                        '                                        <td style="width: '+mindium+'">'+orderABList[i].orderStatus+'</td>\n' +
                        '                                        <td style="width: '+mindium+'">'+orderABList[i].createDate+'</td>\n' +
                        '                                        <td style="width: '+mindium+'">'+orderABList[i].receiveUserName+'</td>\n' +
                        '                                    </tr>')
                }

                tableScroll('tableId', tableHeight, 30, 10);
                tableScroll('tableAB', tableHeight, 30, 10);
                layer.closeAll('loading');
            },
            error: function(XMLHttpRequest, textStatus, errorThrown) {
                layer.alert('网络出错!')
            },

            xhrFields: {
                withCredentials: true
            }
        });


    })




    function showDateTime() {
        var dateTimeStr=new Date();
        var finalStr=dateTimeStr.getFullYear()+"-"+(dateTimeStr.getMonth()+1)+"-"+dateTimeStr.getDate()+" ";
        finalStr+=dateTimeStr.getHours()+":"+dateTimeStr.getMinutes()+":"+dateTimeStr.getSeconds();
        $(".date").text(finalStr);
    }
    setInterval(showDateTime, 1000);
    setInterval(htmlReload, 1800000);//30分钟刷新一次

    function htmlReload(){
        window.location.reload()
    }
}



// 参数1 tableID,参数2 div高度，参数3 速度，参数4 tbody中tr几条以上滚动


var MyMarhq;

function tableScroll(tableid, hei, speed, len) {
    $('#' + tableid).parent().find('.tableid_').remove()
    $('#' + tableid).parent().prepend(
        '<table id="'+tableid+'1'+'"><thead>' + $('#' + tableid + ' thead').html() + '</thead></table>'
    ).css({
        'position': 'relative',
        'overflow': 'hidden',
        'height': hei + 'px'
    })
    $("#"+tableid+"1").find('th').each(function(i) {
        $(this).css('width', $('#' + tableid).find('th:eq(' + i + ')').width());
    });
    $("#"+tableid+"1").css({
        'position': 'absolute',
        'top': 0,
        'left': 0,
        'z-index': 9
    })
    $('#' + tableid).css({
        'position': 'absolute',
        'top': 0,
        'left': 0,
        'z-index': 1
    })

    if ($('#' + tableid).find('tbody tr').length > len) {
        $('#' + tableid).find('tbody').html($('#' + tableid).find('tbody').html() + $('#' + tableid).find('tbody').html());
        $(".tableid_").css('top', 0);
        $('#' + tableid).css('top', 0);
        var tblTop = 0;
        var outerHeight = $('#' + tableid).find('tbody').find("tr").outerHeight();

        function Marqueehq() {
            if (tblTop <= -outerHeight * $('#' + tableid).find('tbody').find("tr").length) {
                tblTop = 0;
            } else {
                tblTop -= 1;
            }
            $('#' + tableid).css('margin-top', tblTop + 'px');
            MyMarhq = setTimeout(function() {
                Marqueehq()
            }, speed);
        }
        MyMarhq = setTimeout(Marqueehq, speed);

    }














}

